﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Data;
using System.Data.Common;

namespace NoORM.Core.DbAcceess
{
    public static class DataReaderExtend
    {
        public static List<Dictionary<string,object>> ToJsonList(this IDataReader dataReader)
        {
            List<Dictionary<string, object>> list = new List<Dictionary<string, object>>();
            while (dataReader.Read())
            {
                Dictionary<string, object> dic = new Dictionary<string, object>();
                for(int i = 0; i < dataReader.FieldCount; i++)
                {
                    object o = dataReader[i];
                    dic.Add(dataReader.GetName(i), o);
                }
                list.Add(dic);
            }
            return list;
        }

        public static Dictionary<string,object> ToJson(this IDataReader dataReader)
        {
            Dictionary<string, object> dic = new Dictionary<string, object>();
            if (dataReader.Read())
            {
                for (int i = 0; i < dataReader.FieldCount; i++)
                {
                    object o = dataReader[i];
                    if (o is DateTime)
                    {
                        o = Convert.ToDateTime(o).ToString("yyyy-MM-dd HH:mm:ss");
                    }
                    dic.Add(dataReader.GetName(i), o);
                }
            }
            return dic;
        }
    }
}
